package com.motorhome.h5.controller;

import com.motorhome.h5.common.statics.H5Statics;
import com.motorhome.h5.service.WeixinService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: John
 */
@Controller
@RequestMapping("/wx")
public class WeixinController {

    @Autowired
    private WeixinService weixinService;

    @RequestMapping(value = "/index")
    public void index(HttpServletRequest request, HttpServletResponse response) {
        weixinService.wxLogin(request, response);
    }

    @RequestMapping(value = "/checkSignature")
    public @ResponseBody String checkSignature(HttpServletRequest request) {
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");

        String signStr = "nonce=" + nonce + "token=" + H5Statics.MP_TOKEN + "timestamp=" + timestamp;
        String signature2 = DigestUtils.sha1Hex(signStr);

        if (signature2.equals(signature)) {
            return echostr;
        }
        return null;
    }

}
